package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTask;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 含有未批阅作业数量
 */
@CrossOrigin
@RestController
public class WorkTotalGetController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskClassRepository homeworkTaskClassRepository;
    
    @Autowired
    HomeworkTaskRepository homeworkTaskRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/total", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
    	String userId = null;
    	String schoolId = null;
        try {
            userId = getProfile(request, response).getId();
            schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        
        List<HomeworkTask> homeworkTaskList = homeworkTaskRepository.findByPublishUserIdAndSchoolId(userId, schoolId);
        List<String> taskIds = new ArrayList<>();
        if (homeworkTaskList != null && homeworkTaskList.size() > 0) {
        	for (HomeworkTask homeworkTask : homeworkTaskList) {
        		// 排除通知类作业不用用户答题的作业
				if (!(homeworkTask.getTaskType().equals("2") && !homeworkTask.getIfNeedSumbit())) {
					taskIds.add(homeworkTask.getId());
				}
			}
        }
        
        if (taskIds.size() > 0) {
            Integer total = homeworkTaskClassRepository.countByWorkTotal(userId, schoolId, taskIds);
            respModel.total = total == null ? 0 : total;
        }
        
        return respModel;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // total
        public Integer total;
    }
}
